<template>
	<view class="wrap">
		<u-form :model="dataForm" ref="uForm">
			<u-form-item label-position="left" label="加分类型:" prop="type" label-width="20%">
				<text v-if="!dataForm.bonusPointsTypeTitle" @click="typeShow = true;">请选择加分类型</text>
				<text v-else @click="typeShow = true;">{{dataForm.bonusPointsTypeTitle}}</text>
				<u-picker mode="selector" v-model="typeShow"  :default-selector="[0]" :range="serviceTypeOptions" range-key="title" @confirm="typeConfirm"></u-picker>
				<u-icon slot="right" name="arrow-right" @click="typeShow = true;"></u-icon>
			</u-form-item>
			<u-form-item label-position="left" label="申请原因:" prop="intro" label-width="20%">
				<u-input type="textarea" :border="true" placeholder="请填写申请原因:" v-model="dataForm.content"/>
			</u-form-item>
			<u-form-item label-position="left" label="上传图片:" prop="imgUrl" label-width="20%">
				<u-upload width="160" height="160" :action="uploadUrl" :header="headers" :fileList="fileList"
					@on-success="success" @on-remove="uploadDelete" :deletable="true">
				</u-upload>
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">提交申请</u-button>
		<tabbar-footer></tabbar-footer>
	</view>
	
</template>

<script>
	import {
		getServiceTypes,
		saveServiceAppointment
	} from '@/api/community/serviceDetails.js'
	import tabbarFooter from '../tabbar/tabbar-footer.vue'
	export default {
		data() {
			return {
				uploadUrl: 'http://127.0.0.1:8181/credit-warning-system/piclocal/upload/pic',
				headers: {
					token: uni.getStorageSync('token')
				},
				fileList: [],
				dataForm: {
					bonusPointsTypeId: '',
					bonusPointsTypeTitle: '',
					content: '',
					imgUrlLists: []
				},
				typeShow: false,
				serviceTypeOptions: []
			}
		},
		onLoad() {
			this.getServiceTypeOptions()
		},
		components: {
		  tabbarFooter
		},
		methods: {
			success(data, index, lists, name) {
				console.log(data.data.imgAddress)
				this.dataForm.imgUrlLists.push(data.data.imgAddress)
				console.log(this.dataForm.operationImageList)
			},
			// 图标删除回调函数
			uploadDelete(index, lists, name) {
				console.log(index)
				this.dataForm.imgUrlLists.splice(index, 1)
			},
			typeConfirm(e) {
				console.log('type:' + this.serviceTypeOptions[e].typeName)
				this.dataForm.bonusPointsTypeTitle = this.serviceTypeOptions[e].title
				this.dataForm.bonusPointsTypeId = this.serviceTypeOptions[e].id
			},
			// 获取社区服务类型下拉列表
			getServiceTypeOptions() {
				getServiceTypes().then(res => {
					this.serviceTypeOptions = res.data
					console.log(this.serviceTypeOptions)
				})
			},
			submit() {
				console.log(this.dataForm)
				saveServiceAppointment(this.dataForm).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '申请已提交！',
							icon: 'success'
						});
						this.dataForm = {}
						this.fileList = []
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						});
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>